package com.button.controller;

import com.button.job.AsyncJob;
import com.button.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api")
public class TestController {

    @Autowired
    AsyncJob asyncJob;

    /**
     * 异步任务与调用不在同一个类中
     * @return
     */
    @GetMapping("/success")
    public String getSuccessInfo() {
        asyncJob.successAsyncJob();
        return "success";
    }

    /**
     * 异步任务与调用在同一个类中
     * @return
     */
    @GetMapping("/same-class")
    public String getSameClass() {
        TestController bean = SpringContextUtil.getBean(TestController.class);
        bean.test();
        return "same-class";
    }

    @Async
    public void test(){
        try {
            log.info("=======开始执行异步处理类========");
            Thread.sleep(5000);
            log.info("=======执行异步处理类完毕========");
        }catch (Exception e){
            log.error("异常. e={}", e);
        }
    }

}
